package leeon.bbsbrower.views;

import ipworks.IPWorksException;
import leeon.bbsbrower.actions.BBSMsgAction;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class AllMsgView extends ViewPart {
	public static final String ID = "bbsbrower.view.allmsg";

	private Text text = null;
		
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {		
		
		//用户内容数据
		text = new Text(parent, SWT.V_SCROLL|SWT.READ_ONLY);
		text.setFont(ApplicationWorkbenchWindowAdvisor.listFont);		
				
		//上下左右的事件
		text.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					actionToFriend();
					e.doit = false;
				} else if (e.keyCode == SWT.ARROW_RIGHT) {
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
	}
	
	
	/**
	 * 回用户主菜单
	 */
	private void actionToFriend() {
		//跳转回到用户操作列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			FriendView frd = (FriendView)page.showView(FriendView.ID);
			frd.refreshData(4);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	public void refreshData() {		
		//设定值
		try {
			String msg = BBSMsgAction.getAllMsg();
			if (msg != null && msg.trim().length() != 0)
				text.setText(msg);
			else {
				text.setText("");
				MessageDialog.openInformation(this.getViewSite().getShell(), "所有消息", "没有消息");
			}
		} catch (IPWorksException e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
			e.printStackTrace();
		}
		ApplicationActionBarAdvisor.setStatusLine();
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		text.setFocus();
	}
}